﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Resn.WhuPub.BLL;
using System.Text;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// CreateEmployeeWindow.xaml 的交互逻辑
        /// </summary>
        public partial class CreateEmployeeWindow
        {
                private readonly GlobalManager _gm = GlobalManager.Instance;

                

                private readonly Dictionary<string, Polygon> BottomPolygons = new Dictionary<string, Polygon>();
                private readonly Dictionary<string, Polygon> TopPolygons = new Dictionary<string, Polygon>();

                private readonly Dictionary<string, bool> provinces = new Dictionary<string, bool>();

                public CreateEmployeeWindow()
                {
                        InitializeComponent();

                        provinces = new Dictionary<string, bool>();
                }



                private void Window_Loaded(object sender, RoutedEventArgs e)
                {
                        FindVisualChild(CanvasPolygonPointer, TopPolygons);
                        FindVisualChild(CanvasPolygon, BottomPolygons);

                        foreach (var polygon in TopPolygons)
                        {
                                polygon.Value.MouseLeftButtonDown += Polygon_MouseLeftButtonDown;
                                polygon.Value.Fill = Brushes.LightSteelBlue;
                                provinces.Add(polygon.Key, false);
                        }

                        foreach (var polygon in BottomPolygons)
                        {
                                polygon.Value.Fill = Brushes.LightSteelBlue;
                        }

                        _gm.Name = string.Empty;
                        _gm.EmployeeLevel = EmployeeLevel.普通用户;
                        _gm.Duty = string.Empty;
                        _gm.Telephone = string.Empty;
                        _gm.QQ = string.Empty;
                        _gm.Email = string.Empty;
                        _gm.Memo = string.Empty;

                        LayoutRoot.DataContext = _gm;
                }

                private void Window_OnMouseDown(object sender, MouseButtonEventArgs e)
                {
                        if (e.ChangedButton == MouseButton.Left)
                        {
                                this.DragMove();
                        }
                }

                /// <summary>
                /// WPF查找元素的子元素
                /// </summary>
                /// <param name="obj"></param>
                /// <returns></returns>
                private void FindVisualChild(DependencyObject obj, Dictionary<string, Polygon> polygons )
                {
                        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                        {
                                dynamic child = VisualTreeHelper.GetChild(obj, i);
                                if (child != null && child is Polygon)
                                {
                                        Polygon polygon = child as Polygon;
                                        polygons.Add(polygon.Tag.ToString(), polygon);
                                }
                        }
                }

                private void Polygon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
                {
                        Polygon polygon = sender as Polygon;
                        string province = polygon.Tag.ToString();

                        if (provinces[province])
                        {
                                provinces[province] = false;
                                TopPolygons[province].Fill = Brushes.LightSteelBlue;
                                BottomPolygons[province].Fill = Brushes.LightSteelBlue;
                        }
                        else
                        {
                                provinces[province] = true;
                                TopPolygons[province].Fill = Brushes.Lime;
                                BottomPolygons[province].Fill = Brushes.Lime;
                        }
                }

                private void AcceptButton_Click(object sender, RoutedEventArgs e)
                {
                        if (String.IsNullOrEmpty(_gm.Name))
                        {
                                MessageBox.Show("账号不能为空");
                                return;
                        }

                        try
                        {
                                StringBuilder sb = new StringBuilder();
                                foreach (var province in provinces)
                                {
                                        if (province.Value)
                                        {
                                                sb.Append(province.Key);
                                                sb.Append(";");
                                        }
                                }

                                string md5password =
                                Encoding.Default.GetString(MD5.Create().ComputeHash(Encoding.Default.GetBytes("123456")));
                                Employee employee = Employee.CreateEmployee(_gm.Name, 
                                                                            md5password, 
                                                                            _gm.EmployeeLevel,
                                                                            _gm.Duty,
                                                                            EmployeeState.在职, 
                                                                            _gm.Telephone,
                                                                            _gm.QQ, 
                                                                            _gm.Email, 
                                                                            sb.ToString(), 
                                                                            _gm.Memo);
                                employee.Insert();

                                _gm.Employees.Add(employee);

                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                                return;
                        }

                        finally
                        {
                        }

                        Close();
                }
        }
}